<?php $id = getId(); ?>
<form method="post" id="f<?php _e($id);?>">
<div class="x-list-title">
	<table width="100%" border="0" height="100%">
	<tr>
	<td align="left">
		<h2>Danh sách cơ hội</h2>
	</td>
	<td align="right">	
		<div class="x-list-options">
			<?php _e( tpl_search_form(baseUrl()."/Crm/Opportunity/Search", "#Crm/Opportunity/Search") );?>						
						
			<div class="x-options-container">
				<a class="x-button">Tạo mới  <span class="x-tri">&#9660;</span></a> 
				<div class="x-options">
					<a class="x-options-item" href="#Crm/Customer/Add">Công ty</a>
					<a class="x-options-item" href="#Crm/Contact/Add">Liên hệ</a>
					<a class="x-options-item" href="#Crm/Opportunity/Add">Cơ hội</a>
					<a class="x-options-item" href="#Crm/Contract/Add">Hợp đồng</a>
				</div>
			</div>
			
			<div class="x-options-container">
				<a class="x-button">Giai đoạn bán hàng  <span class="x-tri">&#9660;</span></a> 
				<div class="x-options">
					<?php foreach( $this->get('cats') as $k => $a ):?>
					<a class="x-options-item" href="#Crm/Opportunity?parent_id=<?php _e($a['ID']);?>"><?php _e($a['title']);?></a>
					<?php endforeach;?>
				</div>
			</div>
			
			<div class="x-options-container">
				<a class="x-button">Tác vụ khác  <span class="x-tri">&#9660;</span></a> 
				<div class="x-options">
					<a onclick="module_ajax('<?php _ctrUrl($this);?>/Delete','f<?php _e($id);?>',this)"  class="x-options-item x-track">Xóa đã chọn</a>
					<a class="x-options-item" onclick="load_inframe('<?php _ctrUrl($this);?>/Export',{ title:'Xuất file', height:'300px',dataForm: 'form#f<?php _e($id);?>' })">Xuất ra Exel </a>
				</div>
			</div>
		</div>
	</td>
	</tr>
	</table>	
</div>

<?php 
	$vars = $this->get('vars');
	$rlink = '#Crm/Opportunity';
?>
<table width="100%" cellpadding="0" cellspacing="0" class="x-list" >
	<tr>
		<th width="25" align="center">TT</th>
		<th width="70">
			<?php show_order_link('Mã','code',$rlink,$vars) ;?>
		</th>
		<th>
			<?php show_order_link('Tên cơ hội','name',$rlink,$vars) ;?>
		</th>
		<th width="200">
			<?php show_order_link('Công ty','customer_id',$rlink,$vars) ;?>
		</th>
		<th width="100">
			<?php show_order_link('KV','expect',$rlink,$vars) ;?>
		</th>
		<th width="50">
			<?php show_order_link('XS','probability',$rlink,$vars) ;?>
		</th>
		<th width="80">
			<?php show_order_link('Chăm sóc','care_by_id',$rlink,$vars) ;?>
		</th>				
		<th width="50" colspan="2">Tác vụ</th>
		
		<th align="center" width="25">
			<div class="x-check-container">
				<div class="x-check">
				<input type="checkbox" class="x-checkbox x-checkall"/>
				</div>
				<div class="x-check-options">
					<a class="x-check-item check-all">Chọn tất cả</a>
					<a class="x-check-item uncheck-all">Bỏ chọn tất cả</a>
				</div>
			</div>
		</th>
	</tr>
	<?php if( count($this->get('posts')) == 0):?>
	<tr>
	<td class='page' colspan='10'>
		<div class='x-page'>Không tìm thấy bản ghi nào</div>
	</td></tr>
	<?php else:?>
	<?php foreach( $this->get('posts') as $k => $a ):?>
		<tr>
			<td align="center">
				<?php _e($k+1);?>
			</td>
			<td align="center">
				<?php _e($a['code']);?>
			</td>
			<td>
				<a href="#Crm/Opportunity/View?ID=<?php _e($a['ID']);?>"><?php _e($a['title']);?></a>
			</td>
			<td>
				<a href="#Crm/Customer/View?ID=<?php _e($a['customer_id']);?>"><?php _e(short_title($a['customer_name'],32));?></a>
			</td>
			
			<td align="right">
				<?php _e(show_number($a['expect']));?> <?php _e( $a['currency_symbol']);?>
			</td>
			<td align="center">
				<?php _e($a['probability']);?> %
			</td>

			<td align="center">
				<?php _e(get_user_link($a['care_by_id'],$a['care_by_name'],$a['care_is_deleted']));?>
			</td>

			
			<td width="25" align="center">
				<?php if(  $a['created_by_id'] == get_user_id() || $this->hasRole('crm_admin') || $a['care_by_id'] == get_user_id()  ){?>
				<a href="#Crm/Opportunity/Edit?ID=<?php _e($a['ID']);?>" class="x-edit"></a>
				<?php }else{?>
				<a class="x-noedit"></a>
				<?php }?>
			</td>
			<td width="25" align="center">
				<?php 
					unset($vars['p']);
					$url = 'Crm/Opportunity'.( count($vars) > 0 ? '?'.to_query_configs($vars,true) : '');
					$url = rawurlencode($url);
				?>
				<?php if(  $a['created_by_id'] == get_user_id() || $this->hasRole('crm_admin') ){?>
				<a onclick="module_delete('<?php _baseUrl()?>/Crm/Opportunity/Delete?ID[]=<?php _e($a['ID']);?>&url=<?php _e($url)?>')" class="x-delete"></a>
				<?php }else{?>
				<a class="x-nodelete"></a>
				<?php }?>
			</td>
			<td align="center">
				<input name="ID[]" value="<?php _e($a['ID'])?>" type="checkbox" class="x-checkbox x-checkall"/>
			</td>
		</tr>
	<?php endforeach;?>
		<tr>
			<td colspan="10" class="page">
				<div class="x-page">
					<?php $this->e('page');?>
				</div>		
			</td>
		</tr>
	<?php endif;?>
	
</table>
</form>
